﻿using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Uportal.Shp.Core.Common
{
    /// <summary>
    /// Утилиты по работе с Event Receivers
    /// </summary>
    public static class EventReceiverHelper
    {
        private const int DefaultSequenceNum = 10000;

        // регистрация Event-Receiver
        public static void RegisterEvent(SPList list, Type eventReceiverClass, SPEventReceiverType eventReceiverType, SPEventReceiverSynchronization sync, string name)
        {
            RegisterEvent(list, eventReceiverClass, eventReceiverType, sync, name, DefaultSequenceNum);
        }
        public static void RegisterEvent(SPList list, Type eventReceiverClass, SPEventReceiverType eventReceiverType, SPEventReceiverSynchronization sync, string name, int sequenceNumber)
        {
            string sAssemblyName = eventReceiverClass.Assembly.FullName;
            string sClassName = eventReceiverClass.FullName;
            SPEventReceiverDefinition eventDef = list.EventReceivers.Add();
            eventDef.Type = eventReceiverType;
            eventDef.Assembly = sAssemblyName;
            eventDef.Class = sClassName;
            eventDef.Name = name;
            eventDef.SequenceNumber = sequenceNumber;
            eventDef.Synchronization = sync;
            eventDef.Update();
        }

        // Удаление Event-Receiver
        public static void UnRegisterEvent(SPList list, Type eventReceiverClass)
        {
            string sClassName = eventReceiverClass.FullName;

            SPEventReceiverDefinitionCollection eventReceivers = list.EventReceivers;
            int count = 0;
            bool bDeleted = false;
            while ((!bDeleted) && (count < 100))
            {
                count++;
                bDeleted = true;
                foreach (SPEventReceiverDefinition def in eventReceivers)
                {
                    if (def.Class == sClassName)
                    {
                        def.Delete();
                        bDeleted = false;
                        break;
                    }
                }
            }
        }
        public static void UnRegisterEvent(SPList list, Type eventReceiverClass, SPEventReceiverType eventReceiverType)
        {
            string sClassName = eventReceiverClass.FullName;

            SPEventReceiverDefinitionCollection eventReceivers = list.EventReceivers;
            int count = 0;
            bool bDeleted = false;
            while ((!bDeleted) && (count < 100))
            {
                count++;
                bDeleted = true;
                foreach (SPEventReceiverDefinition def in eventReceivers)
                {
                    if ((def.Class == sClassName) && (def.Type == eventReceiverType))
                    {
                        def.Delete();
                        bDeleted = false;
                        break;
                    }
                }
            }
        }
        public static void UnRegisterEvent(SPList list, Type eventReceiverClass, SPEventReceiverType eventReceiverType, string name)
        {
            string sClassName = eventReceiverClass.FullName;

            SPEventReceiverDefinitionCollection eventReceivers = list.EventReceivers;
            int count = 0;
            bool bDeleted = false;
            while ((!bDeleted) && (count < 100))
            {
                count++;
                bDeleted = true;
                foreach (SPEventReceiverDefinition def in eventReceivers)
                {
                    if ((def.Class == sClassName) && (def.Type == eventReceiverType) && (string.Equals(def.Name, name, StringComparison.OrdinalIgnoreCase)))
                    {
                        def.Delete();
                        bDeleted = false;
                        break;
                    }
                }
            }
        }

        // поиск Event-Receiver
        public static bool ExistsEvent(SPList list, Type eventReceiverClass)
        {
            string sClassName = eventReceiverClass.FullName;

            foreach (SPEventReceiverDefinition def in list.EventReceivers)
            {
                if (def.Class == sClassName)
                    return true;
            }
            return false;
        }
        public static bool ExistsEvent(SPList list, Type eventReceiverClass, SPEventReceiverType eventReceiverType)
        {
            string sClassName = eventReceiverClass.FullName;

            foreach (SPEventReceiverDefinition def in list.EventReceivers)
            {
                if ((def.Class == sClassName) && (def.Type == eventReceiverType))
                    return true;
            }
            return false;
        }
    }
}
